﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Timers;
using System.Threading;
using System.IO;
using BUS;
using DTO;

namespace WebRaoVat
{
    public class Task
    {
        #region Fields
        System.Timers.Timer timer = null;
        #endregion
        #region Properties
        public string Name { get; set; }
        public bool IsRunning { get; set; }
        public DateTime LastRunTime { get; set; }
        public bool IsLastRunSuccessful { get; set; }
        public double Interval { get; set; }
        public bool Stopped { get; set; }
        //public string FilePath { get; set; }
        public int maTinRaoVat { get; set; }
        public DateTime thoiGianKetThuc { get; set; }
        public DateTime thoiGianBatDau { get; set; }
        //int soLanUpTrenNgay = 5;

        #endregion

        #region Public Constructors
        public Task(double interval)
        {
            this.Interval = interval;
            Initialize();
        }
        #endregion

        #region Public Methods
        public void Start()
        {
            this.Stopped = false;
            this.StartTask();
        }
        public void Stop()
        {
            this.Stopped = true;
        }
        #endregion

        #region Private Methods

        private void Initialize()
        {
            this.Stopped = false;
            timer = new System.Timers.Timer(this.Interval);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
        }

        private void StartTask()
        {
            if (!this.Stopped)
            {
                if ((this.thoiGianBatDau <= DateTime.Now) && (DateTime.Now <= this.thoiGianKetThuc))
                {
                    Thread thread = new Thread(new ThreadStart(Execute));
                    thread.Start();
                }
                else if (DateTime.Now > this.thoiGianKetThuc)
                {
                    this.Stop();
                }
            }
        }

        private void Execute()
        {
            try
            {
                this.IsRunning = true;

                TinRaoVatBUS.capNhatThoiGianUp(this.maTinRaoVat, DateTime.Now);
                    
                this.IsLastRunSuccessful = true;
            }
            catch
            {
                this.IsLastRunSuccessful = false;
                // Handle the exception
            }
            finally
            {
                this.IsRunning = false;
            }
        }
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (!this.IsRunning)
                StartTask();
        }
        #endregion

    }

}
